/*
 * MediaStreamPlayer.h
 *
 *  Created on: 2018年8月2日
 *      Author: zhengboyuan
 */

#ifndef MEDIASTREAMPLAYER_H_
#define MEDIASTREAMPLAYER_H_

#include "MediaPlayer.h"
#include <string>
#include "NetVideoSystem.h"
#include "TCriticalSection.h"


class MediaStreamPlayer: public MediaPlayer, public nvs::StreamSink
{
public:
    MediaStreamPlayer();
    virtual ~MediaStreamPlayer();

    bool Open(nvs::NetVideoSystem* system, nvs::MediaStream* stream);

	bool Reconnect(nvs::NetVideoSystem* system, nvs::MediaStream* stream);

    virtual void Close();

    virtual bool IsOpen();

    virtual void Stop();

	virtual bool Play();

    /// 暂停/恢复
    virtual bool Pause(bool toPause);

    virtual bool SetScale(double scale);

    virtual int64_t GetTime();

    virtual bool Seek(int64_t pts);

protected:
    ///设置媒体格式
    virtual void setFormat(const nvs::StreamFormat& fmt);

    ///发送媒体数据
    virtual int writePacket(const nvs::StreamPacket& packet);

    ///通知发送媒体数据结束
    virtual void endOfStream(int reason);

protected:
    bool isStreamOpen();
    void closeStream();

	void copyFormat(MDecode_Format& dest, nvs::StreamFormat& src);
	int convertCodec(int nvsCodec);

	bool openDecoder(nvs::StreamFormat& format);
	bool isDecoderOpen();
	void closeDecoder();

	void onFoundAudio(int flag);

	nvs::NetVideoSystem* getSystem();

protected:
    nvs::NetVideoSystem* m_system;
    nvs::MediaStream*   m_stream;
    nvs::StreamFormat   m_format;
	
	comn::CriticalSection m_csDecoder;
	bool	m_hasOpenDecoder;
	
};

#endif /* MEDIASTREAMPLAYER_H_ */
